<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Category extends Model
{
    use HasFactory;

    protected $table = 'sp_category';
    protected $primaryKey = 'id';
    public $timestamps = false;

    const REDIS_CATEGORY = 'category_list';//分类redis key
    const H5_FIRST_CATEGORY = 'h5_first_category';//一级分类key
    const H5_SECOND_CATEGORY = 'h5_second_category';//二级分类key
    const H5_ALL_SECOND_CATEGORY = 'h5_all_second_category';//所有二级分类key

    //enabled状态 1显示 0隐藏
    const CATEGORY_STATUS_DISPLAY = 1;//显示
    const CATEGORY_STATUS_HIDE = 0;//隐藏

    //is_delete是否删除  1是 2否
    const CATEGORY_IS_DELETE = 1;//是
    const CATEGORY_NOT_DELETE = 2;//否

    const ALIPAY_RENT_CATEGORY = [
        ['id'=>'RENT_PHONE', 'name'=>'物品租赁-手机'],
        ['id'=>'RENT_COMPUTER', 'name'=>'物品租赁-电脑\平板'],
        ['id'=>'RENT_CAMERA', 'name'=>'物品租赁-数码摄像'],
        ['id'=>'RENT_DIGITAL', 'name'=>'物品租赁-数码其他'],
        ['id'=>'RENT_STATIONERY', 'name'=>'物品租赁-电子词典\电纸书\文化用品'],
        ['id'=>'RENT_CLOTHING', 'name'=>'物品租赁-服装'],
        ['id'=>'RENT_LUGGAGE', 'name'=>'物品租赁-箱包'],
        ['id'=>'RENT_OUTDOOR_EQUIPMENT', 'name'=>'物品租赁-户外装备'],
        ['id'=>'RENT_GYM_EQUIPMENT', 'name'=>'物品租赁-健身器材'],
        ['id'=>'RENT_INSTRUMENT', 'name'=>'物品租赁-乐器'],
        ['id'=>'RENT_MATERNAL_INFANT_GOODS', 'name'=>'物品租赁-母婴用品'],
        ['id'=>'RENT_MATERNAL_INFANT_TOY', 'name'=>'物品租赁-玩具'],
        ['id'=>'RENT_ORNAMENT', 'name'=>'物品租赁-家具饰品'],
        ['id'=>'RENT_COOK_UTENSIL', 'name'=>'物品租赁-厨房/烹饪用具'],
        ['id'=>'RENT_HARDWARE_STORAGE', 'name'=>'物品租赁-收纳整理'],
        ['id'=>'RENT_HARDWARE', 'name'=>'物品租赁-五金/工具'],
        ['id'=>'RENT_OFFICE_ELECTRIC', 'name'=>'物品租赁-办公电器'],
        ['id'=>'RENT_OFFICE_FURNITURE', 'name'=>'物品租赁-办公家具'],
        ['id'=>'RENT_PET', 'name'=>'物品租赁-宠物'],
        ['id'=>'RENT_FESTIVE_GOODS', 'name'=>'物品租赁-节庆用品'],
        ['id'=>'RENT_PHOTOGRAPHY_SERVICE', 'name'=>'物品租赁-婚庆/摄影/摄像服务'],
        ['id'=>'RENT_CAR_GOODS', 'name'=>'物品租赁-汽车/用品/配件/改装'],
        ['id'=>'RENT_HOUSEHOLD_ELECTRIC', 'name'=>'物品租赁-大家电'],
        ['id'=>'RENT_LIFE_ELECTRIC', 'name'=>'物品租赁-生活电器'],
        ['id'=>'RENT_VIRTUAL_GOODS', 'name'=>'物品租赁-游戏相关'],
        ['id'=>'RENT_MEDICAL_INSTRUMENT', 'name'=>'物品租赁-OTC药品/医疗器械/计生用品'],
        ['id'=>'RENT_VIDEO_GAME', 'name'=>'物品租赁-电玩/配件/游戏/攻略'],
        ['id'=>'RENT_GOLD', 'name'=>'物品租赁-珠宝/钻石/翡翠/黄金'],
        ['id'=>'RENT_HOME_CARE', 'name'=>'物品租赁-家庭保健'],
        ['id'=>'RENT_GREEN_PLANT', 'name'=>'物品租赁-鲜花速递/花卉仿真/绿植园艺'],
        ['id'=>'RENT_BRAND_SERVER', 'name'=>'物品租赁-品牌台机/品牌一体机/服务器'],
        ['id'=>'RENT_SECONDHAND_DIGITAL', 'name'=>'物品租赁-二手数码'],
        ['id'=>'RENT_NETWORK_EQUIPMENT', 'name'=>'物品租赁-网络设备'],
        ['id'=>'RENT_KITCHENWARE', 'name'=>'物品租赁-厨房电器'],
        ['id'=>'RENT_ACCESSORIES', 'name'=>'物品租赁-配件'],
        ['id'=>'RENT_INTELLIGENT_EQUIPMENT', 'name'=>'物品租赁-智能设备'],
        ['id'=>'RENT_DIGITAL_LOW', 'name'=>'物品租赁-数码小额'],
        ['id'=>'RENT_UMBRELLA', 'name'=>'物品租赁-雨伞']
    ];

    const ALIPAY_RENTAL_CATEGORY = [
        ['id'=>'3c_mobile', 'name'=>'3C办公-手机'],
        ['id'=>'3c_pc', 'name'=>'3C办公-电脑'],
        ['id'=>'3c_camera', 'name'=>'3C办公-摄影'],
        ['id'=>'3c_office', 'name'=>'3C办公-办公设备'],
        ['id'=>'3c_other', 'name'=>'3C办公-其他'],
        ['id'=>'iot_auto_container', 'name'=>'IOT-售卖柜'],
        ['id'=>'iot_stage', 'name'=>'IOT-驿站'],
        ['id'=>'iot_other', 'name'=>'IOT-其他'],
        ['id'=>'ne_battery', 'name'=>'新能源-电池'],
        ['id'=>'ne_electric_car', 'name'=>'新能源-电动车'],
        ['id'=>'ne_other', 'name'=>'新能源-其他'],
        ['id'=>'general_furniture', 'name'=>'泛行业-家具'],
        ['id'=>'general_tv', 'name'=>'泛行业-家电'],
        ['id'=>'general_other', 'name'=>'泛行业-其他'],
    ];

    const ALIPAY_INSURANCE_CATEGORY = [
        '3C',
        '电器',
        '手机',
        '平板',
        '电动车以及电池',
        '其他',
    ];

    protected function GetOne($cate_id)
    {
        $category = $this->where([['enabled', 1], ['id', $cate_id], ['is_delete', 2]])
            ->select('id', 'name', 'image')
            ->first();
        $category = $category ? $category->toArray() : [];
        return $category;
    }

    /**
     * 获取一级分类
     */
    protected function GetParentCate()
    {
        $category = $this->where([['enabled', 1], ['parentid', 0], ['is_delete', 2]])
            ->select('id', 'name', 'image', 'parentid')
            ->get();
        $category = $category ? $category->toArray() : [];
        return $category;
    }

    //获取指定店铺的一级分类
    protected function GetParentCateById($id)
    {
        $where = array(
            'id' => $id,
            'enabled' => 1,
            'parentid' => 0,
            'is_delete' => 2,
        );
        $category = $this->where($where)
            ->select('id', 'name', 'image', 'parentid')
            ->first();
        $category = $category ? $category->toArray() : [];
        return $category;
    }

    /**
     * 获取二级分类
     */
    protected function GetSonCate($parentid)
    {
        $category = $this->where([['enabled', 1], ['parentid', $parentid], ['is_delete', 2]])
            ->select('id', 'name', 'image', 'parentid')
            ->get();
        $category = $category ? $category->toArray() : [];
        return $category;
    }

    /**
     * 获取全部二级分类
     */
    protected function GetSonAllCate()
    {
        $category = $this->where([['enabled', 1],['parentid','!=',0], ['is_delete', 2]])
            ->select('id', 'name')
            ->get();
        $category = $category ? $category->toArray() : [];
        return $category;
    }

    //添加
    protected function addData($data)
    {
        return $data = $this->insert($data);
    }

    //根据id修改
    protected function editDataById($id, $data)
    {
        $where = array(
            'id' => $id,
        );
        return $data = $this->where($where)->update($data);
    }

}
